package com.bocai.eelly.activitys;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.FindNewProductsAdp;
import com.bocai.eelly.bean.FindNewProductListBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.bocai.eelly.views.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 *搜索新品
 */
public class SearchNewProductsActivity extends BaseActivity implements  XListView.IXListViewListener,AdapterView.OnItemClickListener{
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.search_cancle)
    Button searchCancle;
    @Bind(R.id.search_list)
    XListView searchList;

    private  int page=1;
    private FindNewProductsAdp findNewProductsAdp;
    private List<FindNewProductListBean.DataEntity> dataEntities=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_search, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findNewProductsAdp=new FindNewProductsAdp(this,dataEntities);
        searchList.setAdapter(findNewProductsAdp);
        initEvent();
    }
    private void initEvent() {
        searchList.setXListViewListener(this);
        searchList.setPullLoadEnable(false);
        searchList.setPullRefreshEnable(true);
        searchList.setOnItemClickListener(this);
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    page=1;
                    dataEntities.clear();
                    searchList.setPullLoadEnable(false);
                    getNewProducts();
                }
                return false;
            }
        });
    }
    private void getNewProducts() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().findNewProduct(timeline,sign,Utils.getUserId(this,"id"),page,Utils.PAGESIZE,etSearch.getText().toString(),"","")
                .compose(this.<FindNewProductListBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FindNewProductListBean>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        searchList.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showShort(SearchNewProductsActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(FindNewProductListBean findNewProductListBean) {
                        searchList.stopRefresh();
                        Loading.dismiss();
                        L.d(findNewProductListBean.getMsg());
                        if (Utils.checkResultCode(findNewProductListBean.getReturn_code()))
                        {
                            dataEntities.addAll(findNewProductListBean.getData());
                            findNewProductsAdp.notifyDataSetChanged();
                            if (findNewProductListBean.getData().size() < Utils.PAGESIZE) {
                                searchList.setPullLoadEnable(false);
                            }else{
                                searchList.setPullLoadEnable(true);
                            }
                        }else
                        {
                            T.showLong(SearchNewProductsActivity.this,findNewProductListBean.getMsg());
                        }
                    }
                });
    }
    @Override
    public void onRefresh() {
        page=1;
        dataEntities.clear();
        searchList.setPullLoadEnable(false);
        getNewProducts();
    }

    @Override
    public void onLoadMore() {
        page++;
        getNewProducts();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
